package com.teamin.utils;

import com.mysql.cj.core.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by qfu on 2017/9/19
 * 工具类
 */
public class Utils {

    /**
     * 获取文件的后缀
     * @param fileName
     * @return
     */
    public static String getFileSuffixName(String fileName) {
        if (StringUtils.isNullOrEmpty(fileName)) {
            return null;
        }

        if (fileName.lastIndexOf(".") < 0) {
            return "";
        } else {
            String regex = "[0-9a-zA-Z]{1,10}";
                Pattern pattern = Pattern.compile(regex);
                boolean matcher = pattern.matcher(fileName).find();
                if(matcher) {
                    return fileName.substring(fileName.lastIndexOf(".")+1);
                }else
                    return "" ;
        }
    }

    /**
     * 格式化日期
     *
     * @param time
     * @return
     */
    public static String getFormatDateTime(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(time);
    }

    /**
     * 格式化日期
     *
     * @param time
     * @return
     */
    public static String getFormatDateTimeYYMMDD(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(time);
    }

    /**
     * 获取字符串中@的用户
     * @param s 要提取的字符串
     * @return
     */
    public static List<Long> getAtUserString(String s) {
        List<Long> uList = new ArrayList<>() ;
        Pattern pattern = Pattern.compile("\\`\\^\\-\\>@[\\w\\W]{1,100}?\\([0-9]{1,10}\\)\\<\\-\\$\\`");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
            String g = matcher.group();
            uList.add(Long.parseLong(g.substring(g.lastIndexOf("(")+1,g.lastIndexOf(")")))) ;
        }
        return uList ;
    }

    /**
     * 替换AT的String
     * @return
     */
    public static String replaceAtString(String s) {
        Pattern patternAt = Pattern.compile("\\`\\^\\-\\>@[\\w\\W]{1,100}?\\([0-9]{1,10}\\)\\<\\-\\$\\`");
        Pattern patterNumber = Pattern.compile("@[\\w\\W]{1,30}\\(");
        Matcher matcher = patternAt.matcher(s);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String g = matcher.group();

            String sIndex = Matcher.quoteReplacement(g.substring(g.indexOf("@"), g.lastIndexOf("("))) ;
            //Matcher matterNumber = patterNumber.matcher(g);
            matcher.appendReplacement(sb, sIndex+" ");//当前替之前的字符放入到StringBuffer
        }
        matcher.appendTail(sb);	//将当前替换之后的字符放入到StringBuffer中
        return sb.toString();
    }

    /**
     * 获取剩余时间
     * @param milliseconds
     * @return
     */
    public static String getSurpTimeString(long milliseconds) {
        try {

            Date currDate = new Date(System.currentTimeMillis());
            Date endDate = new Date(milliseconds);

            if(currDate.getTime() > endDate.getTime()) {
                return "" ;
            }

            long diff =  endDate.getTime() - currDate.getTime(); // 得到的差值是微秒级别，可以忽略
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

            System.out.println(days);
            System.out.println(hours);
            System.out.println(minutes);

            return (days>0?days+"天":"")+(hours>0?hours+"小时":"")+(minutes>0?minutes+"分钟":"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null ;
    }

}
